package com.meatball.game.websocket.interceptor

import org.springframework.http.server.ServerHttpRequest
import org.springframework.http.server.ServerHttpResponse
import org.springframework.web.socket.WebSocketHandler
import org.springframework.web.socket.server.HandshakeInterceptor
import java.lang.Exception

class WebsocketHandshakeInterceptor : HandshakeInterceptor {
    override fun beforeHandshake(request: ServerHttpRequest, response: ServerHttpResponse, wsHandler: WebSocketHandler, attributes: MutableMap<String, Any>): Boolean {
        // 从请求的URI中获取参数
        val uri = request.uri
        val params = uri.query?.split("&")?.associate {
            val (key, value) = it.split("=")
            key to value
        }
        // 将参数添加到WebSocket会话的属性中
        if (params != null) {
            attributes.putAll(params)
        }
        return true
    }

    override fun afterHandshake(request: ServerHttpRequest, response: ServerHttpResponse, wsHandler: WebSocketHandler, exception: Exception?) {
    }
}